package com.kol.collect.common;

import java.io.Serializable;

import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;

import com.kol.collect.common.util.FastJsonUtils;

public class AjaxJson<T> implements Serializable {

    private static final long serialVersionUID = 2738165973027758681L;
    // 是否成功
    private String status = "";
    // 提示信息
    private String msg = "操作成功";
    // 返回结果
    private T data = null;

    /**
     * 请求成功
     */
    public static final String STATUS_SUCCESS = "10000";
    public static final String MSG_SUCCESS = "操作成功";
    /**
     * 系统异常
     */

    public static final String STATUS_EXCEPTION = "11111";
    public static final String MSG_EXCEPTION = "系统异常";
    public static final String MSG_INTERFACE_ERROR = "接口异常";

    /**
     * 参数错误
     */
    public static final String STATUS_PARAM_ERROR = "10001";
    public static final String MSG_PARAM_ERROR = "请求参数错误";

    public static final String STATUS_UPDATE_FAILED = "10003";
    public static final String MSG_UPDATE_FAILED = "更新失败";

    public static final String STATUS_REDIS_OP_FAIL = "10005";
    public static final String MSG_REDIS_OP_FAIL = "redis 操作错误";

    /**
     * 认证
     */
    public static final String STATUS_USER_LOGIN_USER_OR_PASS = "11001";
    public static final String MSG_USER_LOGIN_USER_OR_PASS = "用户名或密码错误";
    public static final String STATUS_USER_LOGIN_NO_PHONE = "11003";
    public static final String MSG_USER_LOGIN_NO_PHONE = "手机号或验证码错误";
    public static final String STATUS_USER_HEADER_NO_USER_ID = "11005";
    public static final String MSG_USER_HEADER_NO_USER_ID = "请求头中未包含 userid";
    public static final String STATUS_USER_HEADER_NO_USER_NAME = "11007";
    public static final String MSG_USER_HEADER_NO_USER_NAME = "请求头中未包含 username";
    public static final String STATUS_USER_LOGIN_NO_TOKEN = "11009";
    public static final String MSG_USER_LOGIN_NO_TOKEN = "请求头中未包含 token";
    public static final String STATUS_USER_LOGIN_FAIL_TIMES = "11011";
    public static final String MSG_USER_LOGIN_FAIL_TIMES = "密码输错次数已达 3 次,请一小时后再登录";
    public static final String STATUS_AUTHENTICATION_FAIL = "11013";
    public static final String MSG_AUTHENTICATION_FAIL = "认证失败";
    public static final String STATUS_TOKEN_FAIL = "11015";
    public static final String MSG_TOKEN_FAIL = "token 失效，请重新登录 !";

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }

    /**
     * 参数异常
     *
     * @return
     */
    public static AjaxJson<Object> paramsError() {
        AjaxJson<Object> ajaxJson = new AjaxJson<>();
        ajaxJson.setMsg(AjaxJson.MSG_PARAM_ERROR);
        ajaxJson.setStatus(AjaxJson.STATUS_PARAM_ERROR);
        return ajaxJson;
    }

    /**
     * 成功了
     */
    public static <T> AjaxJson<T> success(T t) {
        AjaxJson<T> ajaxJson = new AjaxJson<>();
        ajaxJson.setMsg(AjaxJson.MSG_SUCCESS);
        ajaxJson.setStatus(AjaxJson.STATUS_SUCCESS);
        ajaxJson.setData(t);
        return ajaxJson;
    }

    public static AjaxJson success() {
        return success(null);
    }

    public static void main(String[] args) {
        AjaxJson<Object> a = new AjaxJson<>();
        // a.setData("123");
        System.out.println(FastJsonUtils.toJSONNoFeatures(AjaxJson.success()));
    }

}
